package com.ruoyi.hospital.medication.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.hospital.medication.domain.TMedication;
import com.ruoyi.hospital.medication.mapper.TMedicationMapper;
import com.ruoyi.hospital.medication.service.ITMedicationService;
import com.ruoyi.hospital.medicine.domain.TPatientMedicine;
import com.ruoyi.hospital.medicine.mapper.TPatientMedicineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 药品管理Service业务层处理
 *
 * @author Shawn
 * @date 2024-02-21
 */
@Service
public class TMedicationServiceImpl implements ITMedicationService {
    @Autowired
    private TMedicationMapper tMedicationMapper;

    @Autowired
    private TPatientMedicineMapper tPatientMedicineMapper;

    /**
     * 查询药品管理
     *
     * @param id 药品管理主键
     * @return 药品管理
     */
    @Override
    public TMedication selectTMedicationById(Long id) {
        return tMedicationMapper.selectTMedicationById(id);
    }

    /**
     * 查询药品管理列表
     *
     * @param tMedication 药品管理
     * @return 药品管理
     */
    @Override
    public List<TMedication> selectTMedicationList(TMedication tMedication) {
        return tMedicationMapper.selectTMedicationList(tMedication);
    }

    /**
     * 新增药品管理
     *
     * @param tMedication 药品管理
     * @return 结果
     */
    @Override
    public int insertTMedication(TMedication tMedication) {
        List<TMedication> tMedicationList = tMedicationMapper.selectTMedicationByName(tMedication.getName());
        if (!tMedicationList.isEmpty()) {
            throw new ServiceException("药品不允许重复");
        }
        tMedication.setCreatedTime(new Date());
        tMedication.setCreatedBy(String.valueOf(ShiroUtils.getUserId()));
        return tMedicationMapper.insertTMedication(tMedication);
    }

    @Override
    public int deductionInventory(TMedication tMedication) {
        if (null == tMedication.getId()) {
            throw new ServiceException("药品ID不能为空");
        }
        TMedication medication = tMedicationMapper.selectTMedicationById(tMedication.getId());
        if (null == medication) {
            throw new ServiceException("药品不存在");
        }
        if (medication.getInventory() < tMedication.getInventory()) {
            throw new ServiceException("药品库存不足");
        }
        medication.setInventory(medication.getInventory() - tMedication.getInventory());
        return tMedicationMapper.updateTMedication(medication);
    }

    @Override
    public int increaseInventory(TMedication tMedication) {
        if (null == tMedication.getId()) {
            throw new ServiceException("药品ID不能为空");
        }
        TMedication medication = tMedicationMapper.selectTMedicationById(tMedication.getId());
        if (null == medication) {
            throw new ServiceException("药品不存在");
        }
        medication.setInventory(medication.getInventory() + tMedication.getInventory());
        return tMedicationMapper.updateTMedication(medication);
    }


    /**
     * 修改药品管理
     *
     * @param tMedication 药品管理
     * @return 结果
     */
    @Override
    public int updateTMedication(TMedication tMedication) {
        return tMedicationMapper.updateTMedication(tMedication);
    }

    /**
     * 批量删除药品管理
     *
     * @param ids 需要删除的药品管理主键
     * @return 结果
     */
    @Override
    public int deleteTMedicationByIds(String ids) {
        String[] array = Convert.toStrArray(ids);
        for (String id : array) {
            checkDelete(id);
        }
        return tMedicationMapper.deleteTMedicationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除药品管理信息
     *
     * @param id 药品管理主键
     * @return 结果
     */
    @Override
    public int deleteTMedicationById(Long id) {
        return tMedicationMapper.deleteTMedicationById(id);
    }

    private void checkDelete(String medicationId) {
        TPatientMedicine tPatientMedicine = new TPatientMedicine();
        tPatientMedicine.setMedicationId(medicationId);
        List<TPatientMedicine> tPatientMedicineList = tPatientMedicineMapper.selectTPatientMedicineList(tPatientMedicine);
        if (!tPatientMedicineList.isEmpty()) {
            throw new ServiceException("已有门诊引用，不能删除药物");
        }
    }
}
